package com.pt25.service.sys.interfaces.facade.inner;

import com.pt25.base.model.security.aspect.PermissionCheck;
import com.pt25.base.res.ResBody;
import com.pt25.base.util.TreeUtil;
import com.pt25.service.sys.domain.test.service.TestTreeService;
import com.pt25.service.sys.interfaces.vo.test.TestTreeVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@Tag(
    name = "test inner api"
)
@RestController
@RequestMapping("/inner-api/sys")
public class TestInnerController {
  private static final String TEST_TREE = "/test-tree";

  private static final String ADD_TEST_TREE = TEST_TREE;

  private static final String DEL_TEST_TREE = TEST_TREE + "/{id}";

  private static final String UPDATE_TEST_TREE = TEST_TREE;

  private static final String GET_TEST_TREE = TEST_TREE + "/{id}";

  private static final String TREE_TEST_TREE = TEST_TREE + "/tree";

  @Resource
  private TestTreeService testTreeService;

  @Operation(summary = "测试数据查询", parameters = {@Parameter(in = ParameterIn.HEADER, name = "X-tenant-ID", description = "租户id", schema = @Schema(type = "string"))})
  @PermissionCheck("sys:test-tree:add")
  @PostMapping(ADD_TEST_TREE)
  public ResBody addTestTree(@RequestBody TestTreeVo testTreeVo) {
    testTreeVo.setId(null);
    boolean result = testTreeService.addTestTree(testTreeVo.toTestTree());
    return result ? ResBody.builder().build().ok() : ResBody.builder().build().fail();
  }

  @Operation(summary = "测试数据查询", parameters = {@Parameter(in = ParameterIn.HEADER, name = "X-tenant-ID", description = "租户id", schema = @Schema(type = "string"))})
  @PermissionCheck("sys:test-tree:del")
  @DeleteMapping(DEL_TEST_TREE)
  public ResBody delTestTree(@PathVariable("id") long id) {
    Boolean result = testTreeService.delTestTree(id);
    return result ? ResBody.builder().build().ok() : ResBody.builder().build().fail();
  }

  @Operation(summary = "测试数据查询", parameters = {@Parameter(in = ParameterIn.HEADER, name = "X-tenant-ID", description = "租户id", schema = @Schema(type = "string"))})
  @PermissionCheck("sys:test-tree:update")
  @PutMapping(UPDATE_TEST_TREE)
  public ResBody updateTestTree(@RequestBody TestTreeVo testTreeVo) {
    boolean result = testTreeService.updateTestTree(testTreeVo.toTestTree());
    return result ? ResBody.builder().build().ok() : ResBody.builder().build().fail();
  }

  @Operation(summary = "测试数据查询", parameters = {@Parameter(in = ParameterIn.HEADER, name = "X-tenant-ID", description = "租户id", schema = @Schema(type = "string"))})
  @PermissionCheck("sys:test-tree:get")
  @GetMapping(GET_TEST_TREE)
  public ResBody getTestTree(@PathVariable("id") Long id) {
    return ResBody.builder().build().ok(testTreeService.getTestTree(id));
  }

  @Operation(summary = "测试数据查询", parameters = {@Parameter(in = ParameterIn.HEADER, name = "X-tenant-ID", description = "租户id", schema = @Schema(type = "string"))})
  @PermissionCheck("sys:test-tree:tree")
  @PostMapping(TREE_TEST_TREE)
  public ResBody treeTestTree() {
    return ResBody.builder().build().ok(TreeUtil.toTree(testTreeService.listTestTree(), 0L));
  }
}
